/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package fr.ove.openmath.mathematica;

/**
 *
 * @author Mr.Sam
 */
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.Vector;
import fr.ove.openmath.jome.Jome;
import fr.ove.openmath.mathematica.*;
import fr.ove.openmath.mathematica.events.*;

public class PanelJomes extends JPanel {
    private BracketBullet bullet;
    
    // Everything about the request
    private JPanel panelRequest;
    private Jome jomeRequest;

    // Everything about the result
    private JPanel panelResult;
    private Jome jomeResult;
    
    // The listeners
    private Vector listeners = new Vector();

    /**
    * The constructor.
    */
	public PanelJomes() {
	    super();
        setBackground(Color.white);
        
	    // On met un BorderLayout.
	    // La zone "North" contiendra la requête
	    // La zone "Center" contiendra le résultat de la requête.
		setLayout(new BorderLayout(0, 0));

        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BorderLayout(0, 0));
        add("Center", mainPanel);
        
		// La partie requête
		panelRequest = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 4));
		panelRequest.setBackground(Color.white);
		
		jomeRequest = new Jome();
		jomeRequest.setDrawBounds(false);
		jomeRequest.setFont(new java.awt.Font("Times New Roman", java.awt.Font.PLAIN, 18));
		jomeRequest.setShiftX(0);
		jomeRequest.setShiftY(0);
        panelRequest.add(jomeRequest);
        
		// La partie résultat
		panelResult = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
		panelResult.setBackground(Color.white);
		
		jomeResult = new Jome();
		jomeResult.setDrawBounds(false);
		jomeResult.setShiftX(0);
		jomeResult.setShiftY(0);
		jomeResult.setFont(new java.awt.Font("Times New Roman",java.awt.Font.PLAIN,12));
		panelResult.add(jomeResult);
        
		mainPanel.add("North", panelRequest);
		mainPanel.add("Center", panelResult);
	    
    	bullet = new BracketBullet(this);
        add("West", bullet);
        
        addMouseListener(
            new MouseAdapter() {
                public void mousePressed(MouseEvent event) {
                	PanelJomesEvent panelJomesEvent = new PanelJomesEvent(PanelJomes.this);
                	panelJomesEvent.setAction(PanelJomesEvent.UPDATE_SELECTION, null);
                	firePanelJomesEvent(panelJomesEvent);
                }
            }
        );
	}
	
	/*
	public Dimension getPreferredSize() {
	    return new Dimension(150, 100);
	}
    */
    
    public boolean isSelected() {
        return bullet.isSelected();
    }
    
    public void setSelected(boolean isSelected) {
        bullet.setSelected(isSelected);
    }
        
	// La partie requête de la chose.

	/**
	* Sets the request. (under its linear form)
	* @param request the request.
	*/
	public void setLinearRequest(String request) {
	    if (!request.equals(jomeRequest.getLinear())) {
    	    jomeRequest.setLinear(request);
    	    panelRequest.validate();
    	    
    	    PanelJomesEvent event = new PanelJomesEvent(this);
    	    event.setAction(PanelJomesEvent.UPDATE_REQUEST, null);
    	    firePanelJomesEvent(event);
    	}
	}

	/**
	* Returns the request. (under its linear form)
	*/
	public String getLinearRequest() {
	    return jomeRequest.getLinear();
	}

	/**
	* Sets the request. (under its OpenMath form)
	* @param request the request.
	*/
	public void setOpenMathRequest(String request) {
    	jomeRequest.setOpenMath(request);
    	panelRequest.validate();
	    
    	PanelJomesEvent event = new PanelJomesEvent(this);
    	event.setAction(PanelJomesEvent.UPDATE_REQUEST, null);
    	firePanelJomesEvent(event);
	}
	
	/**
	* Returns the request. (under its OpenMath form)
	*/
	public String getOpenMathRequest() {
	    return jomeRequest.getOpenMath();
	}

	// La partie résultat de la chose.

	/**
	* Sets the result. (under its linear form)
	* @param result the result.
	*/
	public void setLinearResult(String result) {
	    jomeResult.setLinear(result);
	    panelResult.validate();
	    
	    PanelJomesEvent event = new PanelJomesEvent(this);
	    event.setAction(PanelJomesEvent.UPDATE_RESULT, null);
	    firePanelJomesEvent(event);
	}

	/**
	* Returns the result. (under its linear form)
	*/
	public String getLinearResult() {
	    return jomeResult.getLinear();
	}
	
	public Jome getJomeResult() {
	    return jomeResult;
	}

	public Jome getJomeRequest() {
	    return jomeRequest;
	}

	/**
	* Sets the result. (under its OpenMath form)
	* @param result the result.
	*/
	public void setOpenMathResult(String result) {
    	jomeResult.setOpenMath(result);
    	panelResult.validate();
    	    
    	PanelJomesEvent event = new PanelJomesEvent(this);
    	event.setAction(PanelJomesEvent.UPDATE_RESULT, null);
    	firePanelJomesEvent(event);
	}

	/**
	* Returns the result. (under its OpenMath form)
	*/
	public String getOpenMathResult() {
	    return jomeResult.getOpenMath();
	}
	
    /**
    */
    public void addPanelJomesListener(PanelJomesListener panelJomesListener) {
        listeners.addElement(panelJomesListener);
    }

    /**
    */
    public void removePanelJomesListener(PanelJomesListener panelJomesListener) {
        listeners.removeElement(panelJomesListener);
    }

    /**
    */
    public void removeAllPanelJomesListener() {
        listeners.setSize(0);
    }

    /**
    * Fires a PanelJomesEvent event to registered listeners.
    * @param panelJomesEvent event encapsulating relevant information.
    */
    public void firePanelJomesEvent(PanelJomesEvent panelJomesEvent) {
        for (int i = 0; i < listeners.size(); i++)
          ((PanelJomesListener)listeners.elementAt(i)).consumePanelJomesEvent(panelJomesEvent);
    }
}

